/**
 * axios 网络请求封装
 */
import axios from 'axios'
axios.defaults.withCredentials = true


import qs from 'qs'//qs是一个url参数转化（parse和stringify）的js库
import Vue from 'vue'
import Router from 'vue-router'
import url from './http_conf'
import{MessageBox} from 'mint-ui'
import router from '../router';


const port = '';
const http_url = url + (port == '' ? '' : (':' + port)) + '/'


const instance = axios.create({
  baseURL: http_url,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
  }
});


const isFormData = (v) => {
  return Object.prototype.toString.call(v) === '[object FormData]';
}

/**
 *axios 全局 拦截器
 */

instance.interceptors.response.use(function (response) {

  //忽略返回类型, 取消拦截判断
  // const ignoreContentTypeList = ['image/gif', 'application/vnd.ms-excel;charset=utf-8'];
  // if (ignoreContentTypeList.indexOf(response.headers['content-type']) > -1) return response.data;

  //这里统一处理服务器code
  const Code = response.data.code;
  console.log('code', Code);
  if (Code == 0 || Code == 1001) {
    return response.data
  } else {
    if (Code == 100) {
      MessageBox.alert('登录已过期，请重新登录').then(action => {
        router.push({path:'/login'})
      });
      Vue.prototype.$token = localStorage.token = '';
    } else if(Code == 101) {
        Vue.prototype.$token = localStorage.token = '';
    }else {
        return Promise.reject(response.data);
    }
  }
}, function (error) {
  Vue.prototype.$message.error('请求出错' + error);
  return Promise.reject(error);
});

/**
 * Request
 * @param {String} url
 * @param {Object} options request参数
 * @param {Boolean} tokenFlag 是否置入token,默认为true
 */
export const request = (url, options = {}, tokenFlag = true) => {
  options = {
    url,
    ...options
  }
  return instance.request(options);
}

/**
 * POST
 * @param {String} url
 * @param {Object} data data参数
 * @param {Boolean} tokenFlag 是否置入token,默认为true
 */
export const POST = (url, data = {}, tokenFlag = true) => {
  console.log('post data',data)
  if (localStorage.token && tokenFlag) {
    data = Object.assign(data, {
      token: localStorage.token
    })
  }

  if (!isFormData(data)) data = qs.stringify(data);
  console.log('data',data)
  return instance.post(url, data);
}

/**
 * GET
 * @param {String} url
 * @param {Object} data data参数
 * @param {Boolean} tokenFlag 是否置入token,默认为true
 */
export const GET = (url, data = {}, tokenFlag = true) => {
  if (localStorage.token && tokenFlag) {
    data = Object.assign(data, {
      token: localStorage.token
    })
  }

  if (!isFormData(data)) data = qs.stringify(data);
  console.log('data',data)
  return instance.get(url+'?'+data);
}


/**
 * PUT
 * @param {String} url
 * @param {Object} data data参数
 * @param {Boolean} tokenFlag 是否置入token,默认为true
 */
export const PUT = (url, params = {}, tokenFlag = true) => {
  return instance.put(url, params);
}


/**
 * DELETE
 * @param {String} url
 * @param {Object} data data参数
 * @param {Boolean} tokenFlag 是否置入token,默认为true
 */
export const DELETE = (url, params = {}, tokenFlag = true) => {
  return instance.delete(url, params);
}

export default request
